<template>
  <div class="ai-consultation-layout">
    <!-- 顶部头部 -->
    <div class="ai-header">
      <div class="logo-container">
        <img src="@/assets/logo/logo.svg" class="logo-image" alt="Logo" />
        <span class="logo-text">阡陌智标</span>
      </div>

      <div class="header-actions">
        <!-- <div class="remaining-words-container">
          <div class="remaining-words-info">
            <i class="el-icon-data-line"></i>
            <span class="words-label">剩余字数：</span>
            <span class="word-count">{{ remainingWords }}</span>
          </div>
          <el-button type="primary" class="recharge-btn" @click="showRechargeDialog">
            <i class="el-icon-wallet"></i>
            立即充值
          </el-button>
        </div>
        <div class="action-divider"></div>
        <el-button type="primary" class="subscription-btn">
          <i class="el-icon-office-building"></i>
          订阅企业
        </el-button> -->
        <el-button type="primary" class="logout-btn" plain @click="clearLogin">
          <i class="el-icon-switch-button"></i>
          退出
        </el-button>
        <el-dropdown trigger="click" class="user-dropdown">
          <div class="avatar-wrapper">
            <el-avatar :size="36" src="../src/assets/images/user.png" class="user-avatar"></el-avatar>
            <i class="el-icon-caret-bottom"></i>
          </div>
          <template #dropdown>
            <el-dropdown-menu>
              <el-dropdown-item icon="el-icon-user">个人中心</el-dropdown-item>
              <el-dropdown-item icon="el-icon-setting">账号设置</el-dropdown-item>
              <el-dropdown-item icon="el-icon-refresh">充值记录</el-dropdown-item>
              <el-dropdown-item divided icon="el-icon-switch-button" @click="clearLogin">退出登录</el-dropdown-item>
            </el-dropdown-menu>
          </template>
        </el-dropdown>
      </div>
    </div>

    <!-- 主容器 -->
    <div class="ai-main-container">
      <!-- 左侧边栏 -->
      <div class="ai-sidebar" :class="{ 'collapsed': isCollapsed }">
        <div class="collapse-toggle" @click="toggleCollapse">
          <el-icon v-if="isCollapsed"><Expand /></el-icon>
          <el-icon v-else><Fold /></el-icon>
        </div>
        <div class="menu-container">
          <div
            v-for="route in menuRoutes"
            :key="route.path"
            class="menu-item"
            :class="{ 'active-menu-item': activeMenu === '/dashboard/' + route.path }"
            @click="navigateTo('/dashboard/' + route.path)"
          >
            <el-icon class="menu-icon">
              <component :is="route.meta?.icon || 'Document'"></component>
            </el-icon>
            <span class="menu-text" v-if="!isCollapsed">{{ route.meta?.title }}</span>
          </div>
        </div>

        <div class="bottom-menu-container">
          <div class="divider"></div>
          <div class="menu-item" @click="contactCustomerService">
            <el-icon class="menu-icon"><Service /></el-icon>
            <span class="menu-text" v-if="!isCollapsed">联系客服</span>
          </div>
          <div class="menu-item" @click="businessCooperation">
            <el-icon class="menu-icon"><Promotion /></el-icon>
            <span class="menu-text" v-if="!isCollapsed">商务合作</span>
          </div>
        </div>
      </div>

      <!-- 主内容区域 -->
      <div class="ai-content">
        <div class="content-container">
          <div class="router-view-wrapper">
            <router-view></router-view>
          </div>
        </div>
      </div>
    </div>

    <!-- 底部 -->
    <div class="ai-footer" v-if="!modeStore.isContentMode">
      <div class="footer-content">
        <div class="copyright-info">
          <p>© 2025 深圳市渊维科技有限公司版权所有</p>
        </div>
        <span class="divider">|</span>
        <a href="https://beian.miit.gov.cn/" style="color:#3b82f6" target="_blank">粤ICP备2025384995号-1 </a>
      </div>
    </div>

    <!-- 客服和商务合作弹窗 -->
    <el-dialog v-model="dialogVisible" :title="dialogTitle" width="460px" align-center :close-on-click-modal="false" class="qr-dialog">
      <div class="qr-dialog-content">
        <img src="@/assets/logo/wx.jpg" class="qr-code" alt="QR Code" />
        <p class="qr-description">{{ dialogDescription }}</p>
      </div>
    </el-dialog>

    <!-- 充值弹窗 -->
    <rechargeDialog
      :visible="rechargeDialogVisible"
      :remaining-words="remainingWords"
      :plan-type="planType"
      @cancel="cancelRecharge"
      @recharge-success="handleRechargeSuccess"
    />
  </div>
</template>

<script setup lang="ts">
import { ref, computed, onMounted, inject } from 'vue';
import { useRouter, useRoute } from 'vue-router';
import { Document, Message, Histogram, ChatLineRound, Service, Promotion, User, Expand, Fold } from '@element-plus/icons-vue';
import { ElMessage, ElMessageBox } from 'element-plus';
import {getUserInfo} from '@/api/payment';
import { useUserStore } from '@/store/modules/user';
import rechargeDialog from './components/RechargeDialog/index.vue';
import type { CustomRouteRecordRaw } from '@/router/types';
import { useModeStore } from '@/store/modules/mode';

const userStore = useUserStore();
const router = useRouter();
const route = useRoute();
const activeMenu = computed(() => route.path);
console.log('Current route path:', route.path);
const isCollapsed = ref(false);

const toggleCollapse = () => {
  isCollapsed.value = !isCollapsed.value;
};

const menuRoutes = computed(() => {
  const consultationRoute = router.getRoutes().find((r) => r.path === '/dashboard') as CustomRouteRecordRaw;
  return consultationRoute?.children?.filter((route: CustomRouteRecordRaw) => !route.hidden) || [];
});

const navigateTo = (path: string) => {
  router.push(path);
};

// 计算是否处于正文模式
const modeStore = useModeStore();

// 弹窗相关的响应式变量
const dialogVisible = ref(false);
const dialogTitle = ref('');
const dialogDescription = ref('');
const currentQRCode = ref('');

const contactCustomerService = () => {
  dialogVisible.value = true;
  dialogTitle.value = '联系客服';
  currentQRCode.value = '@/assets/logo/wx.jpg';
  dialogDescription.value = '扫描二维码添加客服微信，获取更多帮助';
};

const businessCooperation = () => {
  dialogVisible.value = true;
  dialogTitle.value = '商务合作';
  currentQRCode.value = '@/assets/logo/wx.jpg';
  dialogDescription.value = '扫描二维码添加商务经理微信，洽谈合作事宜';
};

const clearLogin = () => {
  ElMessageBox.confirm('确认要退出登录吗?', {
    confirmButtonText: '确认',
    cancelButtonText: '取消',
    type: 'warning'
  })
    .then(() => {
      userStore.clearLoginState();
      router.push('/login');
      ElMessage({
        type: 'success',
        message: '退出登录成功！'
      });
    })
    .catch(() => {
      ElMessage({
        type: 'info',
        message: '退出登录失败！'
      });
    });
};

// 充值相关
const rechargeDialogVisible = ref(false);
const remainingWords = ref(0);
const planType = ref("")

const showRechargeDialog = () => {
  rechargeDialogVisible.value = true;
};

const cancelRecharge = () => {
  rechargeDialogVisible.value = false;
};

const handleRechargeSuccess = async () => {
  // 更新用户剩余字数
  const {data} = await getUserInfo();
  remainingWords.value = data.wordLeft
  planType.value = data.membership
};

onMounted(async () => {
  if (route.path === '/dashboard' && menuRoutes.value.length > 0) {
    await router.push('/dashboard/' + menuRoutes.value[0].path);
  }
  const {data} = await getUserInfo();
  remainingWords.value = data.wordLeft
  planType.value = data.membership
});
</script>

<style lang="scss" scoped>
.ai-consultation-layout {
  background: linear-gradient(135deg, #f8fafc 0%, #e0f2fe 100%);
  height: 100vh;
  color: var(--ai-text-primary);
  overflow: hidden;
  display: flex;
  flex-direction: column;
}

// 顶部头部
.ai-header {
  background: linear-gradient(135deg, rgba(255, 255, 255, 0.95) 0%, rgba(255, 255, 255, 0.9) 100%);
  backdrop-filter: var(--ai-blur-md);
  border-bottom: 1px solid var(--ai-border-light);
  padding: 16px 24px;
  display: flex;
  justify-content: space-between;
  align-items: center;
  box-shadow: var(--ai-shadow-md);
  position: relative;
  z-index: 100;
  
}

.logo-container {
  display: flex;
  align-items: center;
  gap: 12px;
}

.logo-image {
  width: 40px;
  height: 40px;
  border-radius: var(--ai-radius-md);
  padding: 8px;
}

.logo-text {
  font-size: 20px;
  font-weight: 700;
  color: var(--ai-text-primary);
  background: var(--ai-gradient-primary);
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  background-clip: text;
}

.header-actions {
  display: flex;
  align-items: center;
  gap: 16px;
}

.remaining-words-container {
  display: flex;
  align-items: center;
  gap: 12px;
  background: var(--ai-bg-subtle);
  padding: 8px 16px;
  border-radius: var(--ai-radius-md);
  border: 1px solid var(--ai-border-light);
}

.remaining-words-info {
  display: flex;
  align-items: center;
  gap: 8px;
  color: var(--ai-text-secondary);
  font-size: 14px;
}

.remaining-words-info i {
  color: var(--ai-primary);
  font-size: 18px;
}

.words-label {
  color: var(--ai-text-secondary);
  font-size: 14px;
}

.word-count {
  color: var(--ai-primary);
  font-weight: 600;
}

.recharge-btn,
.subscription-btn,
.logout-btn {
  background: var(--ai-gradient-primary);
  border: none;
  color: white;
  padding: 8px 16px;
  border-radius: var(--ai-radius-md);
  font-weight: 600;
  cursor: pointer;
  transition: var(--ai-transition);
  box-shadow: var(--ai-shadow-sm);
  
  &:hover {
    transform: translateY(-2px);
    box-shadow: var(--ai-shadow-md);
  }
}

.action-divider {
  height: 24px;
  width: 1px;
  background-color: var(--ai-border-light);
}

.logout-btn {
  background: var(--ai-bg-card);
  color: var(--ai-text-primary);
  border: 1px solid var(--ai-border);
  
  &:hover {
    background: var(--ai-primary);
    color: white;
    border-color: var(--ai-primary);
  }
}

.user-dropdown {
  cursor: pointer;
}

.avatar-wrapper {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 4px 8px;
  border-radius: var(--ai-radius-md);
  transition: var(--ai-transition);
  
  &:hover {
    background: var(--ai-bg-subtle);
  }
}

.avatar-wrapper:hover {
  opacity: 0.9;
}

.user-avatar {
  cursor: pointer;
  border: 2px solid var(--ai-border-light);
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
  transition: all 0.3s ease;
}

.user-avatar:hover {
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);
  transform: scale(1.05);
}

// 主容器
.ai-main-container {
  display: flex;
  flex: 1;
  overflow: hidden;
  min-height: 0;
}

// 左侧边栏
.ai-sidebar {
  background: linear-gradient(180deg, rgba(255, 255, 255, 0.95) 0%, rgba(255, 255, 255, 0.9) 100%);
  backdrop-filter: var(--ai-blur-md);
  width: 240px;
  border-right: 1px solid var(--ai-border-light);
  height: 100%;
  position: relative;
  transition: width 0.3s ease;
  box-shadow: var(--ai-shadow-md);
  overflow: hidden;
  flex-shrink: 0;
}

.ai-sidebar.collapsed {
  width: 64px;
}

.collapse-toggle {
  position: absolute;
  top: 20px;
  right: -12px;
  width: 24px;
  height: 24px;
  background: var(--ai-bg-card);
  border: 1px solid var(--ai-border-light);
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  z-index: 10;
  box-shadow: var(--ai-shadow-sm);
  transition: all 0.3s ease;
  
  &:hover {
    background: var(--ai-primary);
    border-color: var(--ai-primary);
    transform: scale(1.1);
    box-shadow: var(--ai-shadow-md);
    
    .el-icon {
      color: white;
    }
  }
  
  .el-icon {
    font-size: 12px;
    color: var(--ai-text-secondary);
    transition: color 0.3s ease;
  }
}

.menu-container {
  padding: 20px 16px;
  display: flex;
  flex-direction: column;
  gap: 12px;
  flex: 1;
  overflow-y: auto;
  padding-bottom: 160px;
  margin-top: 20px;
  
  &::-webkit-scrollbar {
    width: 4px;
  }
  
  &::-webkit-scrollbar-track {
    background: transparent;
  }
  
  &::-webkit-scrollbar-thumb {
    background: var(--ai-border-light);
    border-radius: 2px;
    
    &:hover {
      background: var(--ai-border);
    }
  }
}

.menu-item {
  display: flex;
  align-items: center;
  padding: 16px 18px;
  border-radius: var(--ai-radius-lg);
  cursor: pointer;
  transition: all 0.3s ease;
  justify-content: flex-start;
  margin: 2px 4px;
  border: 1px solid transparent;
  position: relative;
  overflow: hidden;
  background: var(--ai-bg-card);
  
  &::before {
    content: '';
    position: absolute;
    left: 0;
    top: 0;
    bottom: 0;
    width: 0;
    background: var(--ai-gradient-primary);
    transition: width 0.3s ease;
    z-index: 0;
  }
  
  &:hover {
    border-color: var(--ai-border-accent);
    transform: translateX(4px);
    box-shadow: var(--ai-shadow-md);
    
    &::before {
      width: 4px;
    }
    
    .menu-icon {
      transform: scale(1.1);
    }
  }
  
  &.active-menu-item {
    background: var(--ai-gradient-primary);
    color: white;
    border-color: var(--ai-primary);
    box-shadow: var(--ai-shadow-lg);
    
    .menu-icon {
      color: white;
    }
    
    .menu-text {
      color: white;
    }
  }
}

.ai-sidebar.collapsed .menu-item {
  justify-content: center;
  padding: 16px 0;
  margin: 2px 8px;
}

.menu-icon {
  font-size: 18px;
  color: var(--ai-text-secondary);
  margin-right: 12px;
  z-index: 1;
  position: relative;
  transition: all 0.3s ease;
}

.ai-sidebar.collapsed .menu-icon {
  margin-right: 0;
}

.menu-text {
  font-size: 14px;
  font-weight: 500;
  color: var(--ai-text-primary);
  z-index: 1;
  position: relative;
  transition: all 0.3s ease;
}

.menu-item:hover .menu-icon,
.active-menu-item .menu-icon {
  color: var(--ai-primary);
}

.menu-item:hover .menu-text,
.active-menu-item .menu-text {
  color: var(--ai-primary);
}

.active-menu-item {
  background: var(--ai-gradient-subtle);
  color: var(--ai-primary);
  border-color: var(--ai-primary-light);
  box-shadow: var(--ai-shadow-md);
  transform: translateX(6px);
  
  &::before {
    width: 4px;
  }
  
  .menu-icon {
    color: var(--ai-primary);
    transform: scale(1.1);
  }
  
  .menu-text {
    color: var(--ai-primary);
    font-weight: 600;
  }
}

.active-menu-item .menu-icon,
.active-menu-item .menu-text {
  color: var(--ai-primary);
  font-weight: 500;
}

.bottom-menu-container {
  position: absolute;
  bottom: 0;
  left: 0;
  right: 0;
  background: var(--ai-bg-card);
  border-top: 1px solid var(--ai-border-light);
  padding: 16px;
}

.ai-sidebar.collapsed .bottom-menu-container {
  align-items: center;
  padding: 20px 8px;
}

.divider {
  height: 1px;
  background: var(--ai-border-light);
  margin: 12px 0;
}

// 主内容区域
.ai-content {
  flex: 1;
  overflow: hidden;
  padding: 20px;
  transition: margin-left 0.3s ease;
  display: flex;
  flex-direction: column;
  min-height: 0;
}

.content-container {
  background: linear-gradient(135deg, rgba(255, 255, 255, 0.95) 0%, rgba(255, 255, 255, 0.9) 100%);
  backdrop-filter: var(--ai-blur-md);
  border-radius: var(--ai-radius-xl);
  border: 1px solid var(--ai-border);
  box-shadow: var(--ai-shadow-lg);
  flex: 1;
  overflow: hidden;
  display: flex;
  flex-direction: column;
  min-height: 0;
  position: relative;
  
  &::before {
    content: '';
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    height: 3px;
    background: var(--ai-gradient-primary);
    border-radius: var(--ai-radius-xl) var(--ai-radius-xl) 0 0;
  }
}

.router-view-wrapper {
  flex: 1;
  overflow-y: auto;
  padding: 20px 24px;
  min-height: 0;
  
  &::-webkit-scrollbar {
    width: 8px;
  }
  
  &::-webkit-scrollbar-track {
    background: var(--ai-bg-subtle);
    border-radius: var(--ai-radius-full);
  }
  
  &::-webkit-scrollbar-thumb {
    background: var(--ai-border);
    border-radius: var(--ai-radius-full);
    
    &:hover {
      background: var(--ai-border-dark);
    }
  }
}

.router-view-wrapper::-webkit-scrollbar {
  width: 6px;
}

.router-view-wrapper::-webkit-scrollbar-thumb {
  background: var(--ai-gradient-primary);
  border-radius: 3px;
  border: 1px solid var(--ai-bg-glass);
}

.router-view-wrapper::-webkit-scrollbar-track {
  background: var(--ai-bg-overlay);
  border-radius: 3px;
}

// 底部
.ai-footer {
  background: linear-gradient(135deg, rgba(255, 255, 255, 0.9) 0%, rgba(255, 255, 255, 0.8) 100%);
  backdrop-filter: var(--ai-blur-sm);
  border-top: 1px solid var(--ai-border-light);
  padding: 16px 24px;
  text-align: center;
  color: var(--ai-text-secondary);
  font-size: 14px;
  position: relative;
  
  &::before {
    content: '';
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    height: 1px;
    background: var(--ai-gradient-subtle);
  }
}

.footer-content {
  display: flex;
  justify-content: center;
  align-items: center;
  flex-wrap: wrap;
  gap: 12px;
}

.copyright-info {
  text-align: center;
  display: flex;
  align-items: center;

  p {
    margin: 0;
    font-size: 13px;
    color: var(--ai-text-secondary);
    font-weight: 500;
    line-height: 1;
  }
}

.divider {
  color: var(--ai-text-secondary);
  font-size: 13px;
  line-height: 1;
  display: flex;
  align-items: center;
}

.footer-content a {
  display: flex;
  align-items: center;
  font-size: 13px;
  line-height: 1;
}

// 弹窗样式
:deep(.qr-dialog) {
  border-radius: 16px;
}

:deep(.qr-dialog .el-dialog__header) {
  margin: 0;
  padding: 20px 20px 10px;
  text-align: center;
}

:deep(.qr-dialog .el-dialog__title) {
  font-size: 18px;
  font-weight: 600;
  color: var(--ai-text-primary);
}

:deep(.qr-dialog .el-dialog__body) {
  padding: 0 32px 32px;
}

.qr-dialog-content {
  display: flex;
  flex-direction: column;
  align-items: center;
  padding: 20px 0;
}

.qr-code {
  width: 280px;
  height: 280px;
  margin-bottom: 24px;
  border-radius: 8px;
  box-shadow: 0 2px 12px rgba(0, 0, 0, 0.08);
}

.qr-description {
  color: var(--ai-text-secondary);
  font-size: 15px;
  text-align: center;
  margin: 0;
  line-height: 1.6;
}

// 响应式设计
@media (max-width: 768px) {
  .ai-header {
    padding: 12px 16px;
  }
  
  .logo-text {
    font-size: 18px;
  }
  
  .header-actions {
    gap: 12px;
  }
  
  .remaining-words-container {
    padding: 6px 12px;
  }
  
  .recharge-btn,
  .subscription-btn,
  .logout-btn {
    padding: 6px 12px;
    font-size: 12px;
  }
  
  .ai-sidebar {
    width: 200px;
  }
  
  .ai-content {
    padding: 16px;
  }
}

@media (max-width: 480px) {
  .ai-header {
    padding: 8px 12px;
  }
  
  .logo-image {
    width: 32px;
    height: 32px;
  }
  
  .logo-text {
    font-size: 16px;
  }
  
  .header-actions {
    gap: 8px;
  }
  
  .remaining-words-container {
    display: none;
  }
  
  .ai-sidebar {
    width: 180px;
  }
  
  .ai-content {
    padding: 12px;
  }

  .footer-content {
    flex-direction: column;
    text-align: center;
    gap: 8px;
  }

  .copyright-info {
    text-align: center;
  }
}
</style>
